<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars 
define("PRIMARY_KEY","noref");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_transaction_receipt_report.php";
$FileTemplate  = "ais_transaction_receipt_report_list.html";
$TableName     = "ais_receipt";
$SubHeaderTitle= "Transaction Receipt - Report Receipt";
$PageSize      = 10 ;
$HTMLGridList  = "ReceiptGrid";
//************ FORM ACTION **************
	if (GetParam("Action","")=="Delete") {
		DeleteRecord ("txtCheck",$PageSize);
	}

//Initiate Database Table For Search;
$FCode = GetParam("advCodeSearch","");
//$FSkada = GetParam("skadaSearch","");
//$FDept = GetParam("deptSearch","");
$QueryGet  = GetQueryString("All", array('PageNum'));

//******* DATABASE FORM QUERY ***********

/*
$SQL = "
            SELECT *, sum(jumlah) as sumjumlah 
            FROM `$TableName` 
            LEFT JOIN 
            ais_history_trans ON $TableName.nobkt=ais_history_trans.entry1 AND $TableName.ktrasl=ais_history_trans.ktrasl 
          
          ";
*/

$SQL = "
            SELECT $TableName.*, sum($TableName.jumlah) as sumjumlah, bb_bk.nobkt as aslref,
                        ais_memorial.nobkt as entry3, 
                        sum(ais_memorial.jumlah) as amount3, 
                        IF (L.nobkt<>'',L.nobkt,IF(LZ.nobkt<>'',LZ.nobkt,'')) as entry4, 
                        IF (L.jumlah<>'',L.jumlah,IF(LZ.jumlah<>'',LZ.jumlah,'')) as amount4, 
                        sum(bb_bk.jumlah) as amount2
            FROM `$TableName` 
            INNER JOIN 
            tblreceipt ON $TableName.skada=tblreceipt.skada AND tblreceipt.kdtype=1
            INNER JOIN 
            ais_memorial bb_bk ON $TableName.nokwit=bb_bk.noref AND bb_bk.ktrasl=$TableName.ktrasl AND bb_bk.urut=999
            LEFT JOIN 
            ais_memorial ON bb_bk.nobkt=ais_memorial.noref AND ais_memorial.ktrasl=$TableName.ktrasl AND ais_memorial.urut=1 AND ais_memorial.nobkt like 'M%'
            LEFT JOIN
            ais_memorial L ON L.noref=bb_bk.nobkt AND L.ktrasl=bb_bk.ktrasl AND L.urut=999 AND L.nobkt not like 'M%'
            LEFT JOIN
            ais_memorial_z LZ ON LZ.noref=bb_bk.nobkt AND LZ.ktrasl=bb_bk.ktrasl AND LZ.urut=999  AND LZ.nobkt not like 'M%'

          ";

$SQLCount = "
            SELECT count(*)
            FROM `$TableName` 
            INNER JOIN 
            tblreceipt ON $TableName.skada=tblreceipt.skada AND tblreceipt.kdtype=1
            INNER JOIN 
            ais_memorial bb_bk ON $TableName.nokwit=bb_bk.noref AND bb_bk.ktrasl=$TableName.ktrasl AND bb_bk.urut=999
            LEFT JOIN 
            ais_memorial ON bb_bk.nobkt=ais_memorial.noref AND ais_memorial.ktrasl=$TableName.ktrasl AND ais_memorial.urut=1 AND ais_memorial.nobkt like 'M%'
            LEFT JOIN
            ais_memorial L ON L.noref=bb_bk.nobkt AND L.ktrasl=bb_bk.ktrasl AND L.urut=999 AND L.nobkt not like 'M%'
            LEFT JOIN
            ais_memorial_z LZ ON LZ.noref=bb_bk.nobkt AND LZ.ktrasl=bb_bk.ktrasl AND LZ.urut=999  AND LZ.nobkt not like 'M%'

";

/*
$SQL 		 = "
					SELECT @nokwit = $TableName.nokwit, (SELECT sum(a.jumlah) FROM $TableName a WHERE a.nokwit=@nokwit GROUP BY nokwit)

";
*/

$WHERE   =  " $TableName.ktrasl='".GetOfficeID()."' ";
$WHERE  .=  " AND ifnull(bb_bk.nobkt,'')<>'' ";
$WHERE  .=  " AND ifnull(bb_bk.jumlah,0)-ifnull(ais_memorial.jumlah,0) <> 0 AND IF (L.nobkt<>'',L.nobkt,IF(LZ.nobkt<>'',LZ.nobkt,''))='' ";


/*
$WHERE  .=  " AND $TableName.aslref<>''";
$WHERE  .=  " AND (ifnull(bb_bk.jumlah,0)-ifnull(ais_memorial.jumlah,0)) <> 0";
*/


//Retrieve Parameter from search
$SQLLedger = "SELECT ledger FROM ais_history_ledger ORDER BY ledger DESC LIMIT 1";
$FLedger = (strlen(GetParam("ledger",""))>0)?GetParam("ledger",""):$DBConnection->dbc->get_var($SQLLedger);

if ($FLedger) {
	$CustomWhere = " $TableName.ledger like '".$FLedger."'";
	if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
	else $WHERE = $CustomWhere;	
}

if ($FCode) {
	$CustomWhere = " $TableName.nokwit like '".$FCode."%'";
	if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
	else $WHERE = $CustomWhere;	
}


if (strlen($WHERE)) {
	$SQL = $SQL."WHERE ".$WHERE;
	$SQLCount = $SQLCount."WHERE ".$WHERE;
}

$SQL .= " GROUP BY $TableName.nokwit";
$SQLCount  .= " GROUP BY $TableName.nokwit";
//print $SQL;
//Select Customer 
//$SQLDept = "SELECT * FROM tbldept";

//******* SHOW FORM CONTENT *********
$PageNum = 1;
/*
$RecCnt = $DBConnection->dbc->get_var($SQLCount) ; 
if (!strlen($RecCnt)) $RecCnt = 0;
*/

if (GetParam("PageNum","")) $PageNum  = GetParam("PageNum","");

//Default value
if (!isset($_GET)) $_GET=&$HTTP_GET_VARS ;
if (isset($_GET['PageNum'])) {
  $PageNum = $_GET['PageNum'] ;
} else {
    $PageNum = 1 ;
}

//Default value
if (isset($_GET['RecCnt'])) {
  $RecCnt = intval($_GET['RecCnt']) ;
} else {
    $RecCnt = -1 ;
} 

$TBS = new clsTinyButStrong ;

$SQLLedger = "SELECT * FROM ais_history_ledger ORDER BY ledger DESC ";
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);
$RecCnt = $TBS->MergeBlock("blg",$DBMysql,$SQL,$PageSize,$PageNum,$RecCnt);
//$TBS->MergeBlock("deptblk",$DBMysql,$SQLDept);
$TBS->MergeBlock("ledgerblk",$DBMysql,$SQLLedger);
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize) ; 
$TBS->Show();


function DeleteRecord ($sequence_name,$PageSize) {
	global $DBConnection,$FileName,$TableName;	
	for ($x=1;$x<=$PageSize;$x++) {				
		if (GetParam($sequence_name.$x,""))	{
			$DBConnection->Delete("$TableName",$sequence_name.$x);
	    }
	}
	header("Location: $FileName");
	exit;	
	
}

function ReceiptCodeException($nokwit) {
    global $DBConnection;
    $SQL = "SELECT ais_receipt.skada FROM ais_receipt, tblreceipt WHERE nokwit='$nokwit' AND ais_receipt.thbl=tblreceipt.kdreceipt AND tblreceipt.kdtype=1";
    return $DBConnection->dbc->get_var($SQL);
}


function m_event_blg ($BlockName,&$CurrRec,&$DetailSrc,$RecNum) {
	global $FileName;
        $remain = $CurrRec["amount3"] - $CurrRec["amount2"];
        if ($remain<0) $remain=$remain*-1;
        if (strlen($CurrRec["aslref"]) && ($CurrRec["entry3"]=="") && strlen(ReceiptCodeException($CurrRec["nokwit"])))  {
           $CurrRec["entry3"] = "<a href=$FileName?noref=".$CurrRec["aslref"]."&PageMode=FormMM>MM</a>";
        }

	if ($CurrRec["amount3"] < $CurrRec["amount2"]) { //bb/bk
		if (!strlen($CurrRec["entry4"]) && ($CurrRec["amount3"]>=0)  && ($CurrRec["amount3"]<>'')) {
			$CurrRec["entry4"] = "<a href=$FileName?PageMode=FormBB&noref=".$CurrRec["aslref"]."&remain=$remain>BB</a>/<a href=$FileName?PageMode=FormBK&noref=".$CurrRec["aslref"]."&remain=$remain>BK</a>";
		}
	} elseif ($CurrRec["amount3"] > $CurrRec["amount2"]) { //tb/tk
		if (!strlen($CurrRec["entry4"]) && ($CurrRec["amount3"]>=0)  && ($CurrRec["amount3"]<>'')) {
			$CurrRec["entry4"] = "<a href=$FileName?PageMode=FormTB&noref=".$CurrRec["aslref"]."&remain=$remain>TB</a>/<a href=$FileName?PageMode=FormTK&noref=".$CurrRec["aslref"]."&remain=$remain>TK</a>";
		}	
	}
}

?>